//
//  AddSizeViewController.m
//  quickBatchManager
//
//  Created by 王定波 on 15/9/22.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "AccountDescViewController.h"

@interface AccountDescViewController ()
{
    NSArray *prolist;
}
@end

@implementation AccountDescViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)initUI
{
    UIImage *image = _bgImage.image;
    image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];
    _bgImage.image=image;
    self.view.backgroundColor=[UIColor clearColor];
    
    [_closeBtn bk_whenTapped:^{
           [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    
    
  

}


-(void) initData
{
    if([_orderData isKindOfClass:[BatchProductModel class]])
    {
        BatchProductModel *batch=(BatchProductModel*)_orderData;
        
        _orderNoLabel.text=batch.batchNo;
        _clientNameLabel.text=[NSString stringWithFormat:@"客户：%@",batch.supplier.contactName];
        _dateLabel.text=[NSString stringWithFormat:@"日期：%@",[batch.createDate substringToIndex:10]];
        
        prolist=[AddProductModel searchWithWhere:@{@"batchId":[ToolUtils numberToStr:batch.batchId]} orderBy:nil groupBy:@"productId" offset:0 count:0];
        
        _payStatusLabel.text=(batch.isPay?@"已付款":@"未付款");
        _totalModelLabel.text=[NSString stringWithFormat:@"共%d款",prolist.count];
        _totalNumberLabel.text=[NSString stringWithFormat:@"计：%0.0f件",batch.totalNumber];
        _totalAmountLabel.text=[NSString stringWithFormat:@"总额：%0.2f元",batch.batchPrice];
        _InOutLabel.text=[NSString stringWithFormat:@"退换：%0.0f件 进货：%0.0f件",0.0,batch.totalNumber];
        
        
        
        batch.unpaidPrice=batch.totalPrice-batch.moLingPrice-batch.paidPrice-batch.deptPrice;
        _tsLabel.text= [NSString stringWithFormat:@"%@  %@",![batch.tspayType isValid]?@"":[NSString stringWithFormat:@"%@：%0.2f元",batch.tspayType,batch.paidPrice],batch.unpaidPrice>0?[NSString stringWithFormat:@"未付：%0.2f元",batch.unpaidPrice]:@""];
        [NSString stringWithFormat:@"退换：%0.2f件 进货：%0.0f件",0.0,batch.totalNumber];

        
    }
    if([_orderData isKindOfClass:[SaleOrderModel class]])
    {
        SaleOrderModel *order=(SaleOrderModel*)_orderData;
        
        _orderNoLabel.text=order.saleOrderNo;
        _clientNameLabel.text=[NSString stringWithFormat:@"客户：%@",order.client.clientName];
        _dateLabel.text=[NSString stringWithFormat:@"日期：%@",[order.date substringToIndex:10]];
        
        prolist=[SaleOrderInfoModel searchWithWhere:@{@"saleOrderId":[ToolUtils numberToStr:order.saleOrderId]} orderBy:nil groupBy:@"productId" offset:0 count:0];
        
        _payStatusLabel.text=(order.isPay?@"已付款":@"未付款");
        _totalModelLabel.text=[NSString stringWithFormat:@"共%d款",prolist.count];
        _totalNumberLabel.text=[NSString stringWithFormat:@"计：%0.0f件",order.totalNumber];
        _totalAmountLabel.text=[NSString stringWithFormat:@"总额：%0.2f元",order.orderPrice];
        _InOutLabel.text=[NSString stringWithFormat:@"退换：%0.0f件 进货：%0.0f件",0.0,order.totalNumber];
        
        
        
        order.unpaidPrice=order.totalPrice-order.moLingPrice-order.paidPrice-order.deptPrice;
        _tsLabel.text= [NSString stringWithFormat:@"%@  %@",![order.tspayType isValid]?@"":[NSString stringWithFormat:@"%@：%0.2f元",order.tspayType,order.paidPrice],order.unpaidPrice>0?[NSString stringWithFormat:@"未付：%0.2f元",order.unpaidPrice]:@""];
        [NSString stringWithFormat:@"退换：%0.2f件 进货：%0.0f件",0.0,order.totalNumber];

    }
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return prolist.count;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *reuseIdetify = @"AccountOrderCell";
    AccountOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdetify];
    id model=[prolist objectAtIndex:indexPath.row];
    
    if(!cell){
        
        
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"AccountOrderCell" owner:self options:nil];
        for(id oneObject in nib){
            if([oneObject isKindOfClass:[AccountOrderCell class]]){
                cell = (AccountOrderCell *) oneObject;
                
            }
        }
        
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.backgroundColor=[UIColor clearColor];
    
    
    
    if([model isKindOfClass:[AddProductModel class]])
    {
        AddProductModel *data=[prolist objectAtIndex:indexPath.row];
        cell.label1.text=data.product.modelNo;
        cell.label2.text=[ToolUtils priceToStr:data.buyPrice];
        
        
        double totalnumber=[AddProductModel sumWithWhere:@"productNumber" where:@{@"batchId":[ToolUtils numberToStr:data.batchId],@"productId":[ToolUtils numberToStr:data.productId]}];
        cell.label3.text=[ToolUtils numberToStr:totalnumber];
        cell.label4.text=[ToolUtils priceToStr:totalnumber*data.buyPrice];

    }
    else
    {
        SaleOrderInfoModel *data=[prolist objectAtIndex:indexPath.row];
        cell.label1.text=data.product.modelNo;
        cell.label2.text=[ToolUtils priceToStr:data.price];
        
        
        double totalnumber=[SaleOrderInfoModel sumWithWhere:@"productNumber" where:@{@"saleOrderId":[ToolUtils numberToStr:data.saleOrderId],@"productId":[ToolUtils numberToStr:data.productId]}];
        cell.label3.text=[ToolUtils numberToStr:totalnumber];
        cell.label4.text=[ToolUtils priceToStr:totalnumber*data.price];

    }
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])  {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    
    
    return cell;
    
    
}



@end
